package com.zx.tradeclient.ctrl.index2;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.widget.ExpanderDialog;
import com.zx.base.widget.ExpanderDialog.DialogListener;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class OrderInfoActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private TextView order_info_status;
	private TextView order_info_supply_company;

	private TextView order_info_id;
	private TextView order_info_price;
	private TextView order_info_date;

	private TextView order_info_pay;
	private TextView order_info_product_name;
	private TextView order_info_buy_number;

	private TextView order_info_product_price;
	private TextView order_info_buyer;
	private TextView order_info_buy_phone;

	private TextView order_info_supplyer;
	private TextView order_info_supply_phone;
	private TextView order_info_address;

	private TextView order_info_remark;
	private LinearLayout order_line_state;
	private ProgressDialog progressDialog;
	private ExpanderDialog expander;
	private TradeApp app;

	private String orderID = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_info);

		if (!(ActivityTaskManager.getInstance().containsName("OrderInfoActivity"))) {
			ActivityTaskManager.getInstance().putActivity("OrderInfoActivity", this);
		}

		init();

	}

	private void init() {

		setHeader(0);

		app = (TradeApp) getMyApplication();

		orderID = getIntent().getStringExtra("orderID");

		order_info_status = (TextView) findViewById(R.id.order_info_status);
		order_info_supply_company = (TextView) findViewById(R.id.order_info_supply_company);
		order_info_id = (TextView) findViewById(R.id.order_info_id);
		order_info_price = (TextView) findViewById(R.id.order_info_price);
		order_info_date = (TextView) findViewById(R.id.order_info_date);
		order_info_pay = (TextView) findViewById(R.id.order_info_pay);
		order_info_product_name = (TextView) findViewById(R.id.order_info_product_name);
		order_info_buy_number = (TextView) findViewById(R.id.order_info_buy_number);
		order_info_product_price = (TextView) findViewById(R.id.order_info_product_price);
		order_info_buyer = (TextView) findViewById(R.id.order_info_buyer);
		order_info_buy_phone = (TextView) findViewById(R.id.order_info_buy_phone);
		order_info_supplyer = (TextView) findViewById(R.id.order_info_supplyer);
		order_info_supply_phone = (TextView) findViewById(R.id.order_info_supply_phone);
		order_info_address = (TextView) findViewById(R.id.order_info_address);
		order_info_remark = (TextView) findViewById(R.id.order_info_remark);
		order_line_state =(LinearLayout)findViewById(R.id.order_line_state);
		
		
		
		postSupplyOrderInfo();
	}

	private void postSupplyOrderInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "orderFormDetail");
		map.put("id", orderID);

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				if(Constant.debug)Log.d("TAG",response.toString());
				parseJson(response);
				
			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(OrderInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
			private void parseJson(JSONObject response) {
				try {
					JSONObject object = response;
					String succeed = object.getString("succeed");

					if (succeed.equals("000")) {
						
						order_info_status.setText(object.getString("orderstatus"));
						order_info_supply_company.setText(object.getString("sellerCompanyName"));

						order_info_id.setText(object.getString("orderid"));
						order_info_price.setText(object.getString("totalMoney"));
						order_info_date.setText(object.getString("buyTime"));

						order_info_pay.setText(object.getString("tradeWay"));
						order_info_product_name.setText(object.getString("productName"));
						order_info_buy_number.setText(object.getString("num"));

						order_info_product_price.setText(object.getString("price"));
						order_info_buyer.setText(object.getString("buyerLinkName"));
						order_info_buy_phone.setText(object.getString("buyerLinkTelphone"));

						order_info_supplyer.setText(object.getString("sellerLinkName"));
						order_info_supply_phone.setText(object.getString("sellerLinkTelphone"));
						order_info_address.setText(object.getString("address"));
						order_info_remark.setText(object.getString("remark"));
						
						if(Constant.debug)Log.d("TAG",object.getString("buyerId"));
						if(object.getString("buyerId").equals(app.user.getId()))
						order_line_state.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								// 订单状态
								expander = new ExpanderDialog(OrderInfoActivity.this, new DialogListener() {

									@Override
									public void endme(String group, String value, int groupid, int valueid) {
										//
										changeState(app.ordertypesid[valueid]);
										order_info_status.setText(app.ordertypes[valueid]);
									}
								}, 4);
								expander.show();
							}
						});
						
						

					} else {
						Toast toast2 = Toast.makeText(OrderInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
					}
					

				} catch (JSONException e) {
					
					e.printStackTrace();
				}finally{
					if(progressDialog!=null)
						progressDialog.dismiss();
						progressDialog=null;
				}
			}
		});
	}
	private void changeState(String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "orderFormStateModify");
		map.put("id", orderID);
		map.put("state", id);
		
		
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(OrderInfoActivity.this, "网络超时。",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}

			private void parseJson(JSONObject response) {
				try {
					String succeed = response.getString("succeed");
					if (succeed.equals("000")) {
					
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
	}
	

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		}

	}

}